package com.lz.football.module.leagueMatch.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.LeagueMatchEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.NpcClubInfoVo;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.leagueMatch.item.TeamItem;
	
	import common.item.ComGoodsItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.events.ListEvent;

	/**
	 *预选赛界面 
	 * @author lake
	 * 
	 */	
	public class PreMatchPanel extends Sprite implements ILeagueMatchPanel
	{
		/**
		 * 皮肤
		 */		
		public var _symbolSkin			:MovieClip;
		/**
		 *列表元件 
		 */		
		private var listPanel			:MovieClip;
		/**
		 *描述信息 
		 */		
		private var descMc				:MovieClip;
		/**
		 *球星显示容器 
		 */		
		private var starContainer		:Sprite;
		/**
		 *挑战 
		 */		
		public static var challengeBtn	:SimpleButton;
		/**
		 *列表item 
		 */		
		private var itemList			:Array = [];
		/**
		 *选择的条目 
		 */		
		private var selectItem			:TeamItem;
		/**
		 *进度 
		 */		
		private var txtProgress		:TextField;
		
		public function PreMatchPanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.leaguematch.view.PreMatchPanel");
			this.addChild(_symbolSkin);
			listPanel = _symbolSkin.listPanel;
			descMc = _symbolSkin.descMc;
			txtProgress = _symbolSkin.txtProgress;
			descMc.signBtn.visible = false;
			descMc.signMc.visible = false;
			starContainer = new Sprite();
			descMc.addChild(starContainer);
			starContainer.x = 12;
			starContainer.y = 125;
			challengeBtn = _symbolSkin.challengeBtn;
			var country :int = AppData.getInstance().clubDatamanager.clubInfo.country;
			var clubIds :Array = AppData.getInstance().leagueMatchManager.preMatchIDs[country-1];
			var npcClubVo :NpcClubInfoVo;
			var cid :int;
			var teamItem :TeamItem;
			for(var i :int = 0;i < 5;i++)
			{
				cid = clubIds[i];
				npcClubVo = AppData.getInstance().npcClubManager.getNpcClubById(cid);
				teamItem = new TeamItem();
				teamItem.initUI(listPanel["item_" + i]);
				teamItem.index = i;
				teamItem.data = npcClubVo;
				teamItem.setIconType(1);
				teamItem.addEventListener(ListEvent.ITEM_CLICK,itemClickHandler)
				itemList.push(teamItem);
			}
			selectItem = itemList[0];
			selectItem.select();
			setDescInfo();
			//添加事件
			addEvtListener();
		}
		/**
		 *设置描述信息 
		 */		
		private function setDescInfo():void
		{
			if(selectItem == null)
			{
				return ;
			}
			var npcClubVo :NpcClubInfoVo = selectItem.data;
			descMc.txtName.htmlText = npcClubVo.name + "";
			descMc.txtDesc.htmlText = npcClubVo.descplay + "";
			if(selectItem.isCan)
			{
				challengeBtn.mouseEnabled = true;
				challengeBtn.filters = null;
			}
			else
			{
				//disable
				challengeBtn.mouseEnabled = false;
				challengeBtn.filters = [DisplayObjectUtil.DISABLE_FILTER];
			}
			//球星显示
			DisplayObjectUtil.removeAllChildren(starContainer);
			var starId :int;
			var item :ComGoodsItem;
			var i :int = 0;
			//test
			//npcClubVo.starIds = [Math.random()*800,Math.random()*800,Math.random()*800];
			/*for each(starId in npcClubVo.starIds)
			{
				item = new ComGoodsItem();
				item.initUI();
				item.data = AppData.getInstance().goodsManager.getStarById(starId);
				starContainer.addChild(item);
				item.x = i * (item.width + 3);
				i++;
			}*/
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			challengeBtn.addEventListener(MouseEvent.CLICK,challengeHandler);
			AppData.getInstance().leagueMatchManager.trialMatchInfoVo.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *数据改变 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event):void
		{
			var matchResList :Array = AppData.getInstance().leagueMatchManager.trialMatchInfoVo.matchResList;
			var matchIndex :int = AppData.getInstance().leagueMatchManager.trialMatchInfoVo.matchindex;
			var winNum :int = AppData.getInstance().leagueMatchManager.trialMatchInfoVo.win;
			var teamItem :TeamItem;
			for(var i :int = 0;i<5;i++)
			{
				teamItem = itemList[i];
				if(i < matchIndex)
				{
					teamItem.setScore(matchResList[i].hgoal,matchResList[i].ggoal);
				}
				if(i == matchIndex)
				{
					teamItem.setCurTeam();
				}
				if(i == matchIndex)
				{
					teamItem.isCan = true;
				}
				else
				{
					teamItem.isCan = false;
				}
			}
			selectItem = itemList[matchIndex];
			if(selectItem)
			{
				selectItem.select();
			}
			setDescInfo();
			//进度
			if(txtProgress)
			{
				txtProgress.text = matchIndex + "/5";
			}
		}
		/**
		 *条目点击 
		 * @param e
		 * 
		 */		
		private function itemClickHandler(e :ListEvent):void
		{
			if(selectItem == e.target)
			{
				return;
			}
			if(selectItem)
			{
				selectItem.unSelect();
			}
			selectItem = e.target as TeamItem;
			selectItem.select();
			setDescInfo();
		}
		/**
		 *挑战 
		 * @param e
		 */		
		private function challengeHandler(e :MouseEvent):void
		{
			if(selectItem == null)
			{
				Alert.showErrorTip("未选择挑战队伍");
			}
			var param :Object = {};
			param.cid = selectItem.data.cid;
			this.dispatchEvent(new LeagueMatchEvent(LeagueMatchEvent.PRE_MATCH_START,param,true));
			//disable
			challengeBtn.mouseEnabled = false;
			challengeBtn.filters = [DisplayObjectUtil.DISABLE_FILTER];
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			challengeBtn.removeEventListener(MouseEvent.CLICK,challengeHandler);
			AppData.getInstance().leagueMatchManager.trialMatchInfoVo.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			for each(var teamItem :TeamItem in itemList)
			{
				teamItem.destroyView();
				teamItem.removeEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
				teamItem = null;
			}
			itemList = null;
			selectItem = null;
			_symbolSkin = null;
			listPanel = null;
			descMc = null;
			challengeBtn = null;
			txtProgress = null;
			DisplayObjectUtil.removeAllChildren(starContainer);
			if(starContainer && starContainer.parent)
			{
				starContainer.parent.removeChild(starContainer);
			}
			DisplayObjectUtil.removeAllChildren(this);
		}
		/********************** implements interface *********************************/
		/**
		 * 比赛开始
		 */		
		public function matchStart(value :Object = null):void
		{
			
		}
		/**
		 * 比赛结束
		 */		
		public function matchEnd(value :Object = null):void
		{
			setDescInfo();
		}
		/**
		 * 错误返回
		 */		
		public function errorRes(value :Object = null):void
		{
			challengeBtn.mouseEnabled = true;
			challengeBtn.filters = null;
		}
	}
}